//
//  MCPersonalViewController.swift
//  StoreManage
//
//  Created by MC on 2018/6/20.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCPersonalViewController: MCViewController {
    
    private let dataArray = ["我的佣金","我的分销","核销","配送社区"]
    private let imageArray =  [#imageLiteral(resourceName: "Mine_commission"),#imageLiteral(resourceName: "Mine_distribution"),#imageLiteral(resourceName: "Mine_checkOrder"),#imageLiteral(resourceName: "Mine_community")]
    private var dataDict : [String:Any]?     // 用户信息接口返回的对象
    
    deinit {
        MCNotification_remove(vc: self, name: NOTIFICATION_ChangeStore)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        sendNetworking()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - Setter & Getter
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(19)
        label.text = ""
        label.textAlignment = NSTextAlignment.center
        label.textColor = UIColor.white
        label.frame = CGRect.init(x: 0, y: 0, width: 10, height: 44)
        return label
    }()

    
    fileprivate lazy var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.isScrollEnabled = false
        return tableView
    }()
    
    lazy var topView: MCPersonalTopView = {
        let view = MCPersonalTopView()
        view.editButton.addTarget(self, action: #selector(editButtonClicked), for: .touchUpInside)
        view.editAreaButton.addTarget(self, action: #selector(editButtonClicked), for: .touchUpInside)

        return view
    }()
}



//MARK: 通知回调，闭包回调，点击事件
extension MCPersonalViewController {
    @objc func changeStoreEvent() {
        sendNetworking()
    }
    
    
    @objc func rightItemClicked() {
        let vc = MCSettingViewController()
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
    }
    
    @objc func editButtonClicked() {
        let vc = MCUserinfoViewController()
        vc.dataDict = dataDict
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
    }
}


//MARK: 网络请求
extension MCPersonalViewController {
    
    func sendNetworking() {
        let para = [
            "baUserId" : MCUserinfo_getUserId(),
            ] as [String : Any]
        
        MCNetwork.POST(JAVA_getStoreUserByIdAndStoreId, para, vc: self, success: { (data) in
            let dataDict = data.dictionaryObject ?? [:]
            self.dataDict = dataDict
            
            self.topView.dataDict = dataDict
        }, failure: nil)
    }
}

//MARK: UI的处理
extension MCPersonalViewController {
    // MARK: - Method
    func baseSetting() {
        self.view.backgroundColor = UIColor.white
        self.navigationItem.leftBarButtonItem = nil
        
        MCNotification_addObserver(name: NOTIFICATION_ChangeStore, vc: self, selector: #selector(changeStoreEvent))
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Mine_setting"), target: self, selector: #selector(rightItemClicked))
        
        navigationBarLineHidden = true
        
        navigationBarBackgroundImage = UIImage.init(color: UIColor.MCMain)
    }
    
    func initUI() {
        
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        tableView.tableHeaderView = topView
    }
}

extension MCPersonalViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.backgroundColor = UIColor.white
        
        cell.textLabel?.font = MCFont(14)
        cell.textLabel?.text = dataArray[indexPath.row]
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        cell.imageView?.image = imageArray[indexPath.row]
        
        let line = UIView()
        line.backgroundColor = UIColor.MCGray_light
        cell.contentView.addSubview(line)
        line.snp.remakeConstraints { (make) -> Void in
            make.right.bottom.equalTo(cell.contentView)
            make.left.equalTo(cell.contentView).offset(0)
            make.height.equalTo(1)
        }
        
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let name = dataArray[indexPath.row]
        switch name {
        case "我的佣金":
            let vc = MCMyCommissionViewController()
            vc.clerkId = MCUserinfo_getUserId()
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
        case "我的分销":
            let vc = MCDistributionPageViewController()
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
        case "核销":
            let vc = MCCheckOrderViewController()
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
        case "配送社区":
            let vc = MCCommunityViewController()
            vc.clerkId = MCUserinfo_getUserId()
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
            
        default:
            break
        }
    }
}





